package com.hdcircles.autoform.util;

import com.hdcircles.autoform.model.http.ApiException;
import com.hdcircles.autoform.model.http.HttpResponse;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by hdcircles on 2016/9/23.
 */

public class RxUtil {

    /**
     * 统一线程处理
     */
    public static <T> Observable.Transformer<T, T> rxScheduleHelper() {
        //compose 简化线程
        return new Observable.Transformer<T, T>() {
            @Override
            public Observable<T> call(Observable<T> tObservable) {
                return tObservable.subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread());
            }
        };
    }

    /**
     * 统一返回结果处理
     */
    public static <T> Observable.Transformer<HttpResponse<T>, T> handleResult() {
        return new Observable.Transformer<HttpResponse<T>, T>() {
            @Override
            public Observable<T> call(Observable<HttpResponse<T>> httpResponseObservable) {
                return httpResponseObservable.flatMap(new Func1<HttpResponse<T>, Observable<T>>() {
                    @Override
                    public Observable<T> call(final HttpResponse<T> tHttpResponse) {
//                        if (!tHttpResponse.isError()){
//                            return createData(tHttpResponse.getResults());
//                        }else {
//                            return Observable.error(new ApiException("服务器返回错误!"));
//                        }
                        if (tHttpResponse.getRtncode().equals("01")) {
//                            String error = (String) tHttpResponse.getDesc();
//                            LogUtil.d(error);
                            return Observable.error(new ApiException("没有数据"));
                        } else if (tHttpResponse.getRtncode().equals("99")) {
//                            String error = (String) tHttpResponse.getDesc();
//                            LogUtil.d(error);
                            return Observable.error(new ApiException("服务器异常"));
                        } else if (null != tHttpResponse.getUserList()) {
                            return createData(tHttpResponse.getUserList());
                        } else if (null != tHttpResponse.getFormList()) {
                            return createData(tHttpResponse.getFormList());
                        } else if (null != tHttpResponse.getUser()) {
                            return createData(tHttpResponse.getUser());
                        } else if (null!=tHttpResponse.getQuestionList()){
                          return createData(tHttpResponse.getQuestionList());
                        } else if (null!=tHttpResponse.getChooseQuestionList()){
                            return createData(tHttpResponse.getChooseQuestionList());
                        } else {
                            return createData(tHttpResponse.getDesc());
                        }
                    }
                });
            }
        };
    }

    /**
     * 生成Observable
     */
    public static <T> Observable<T> createData(final T t) {
        return Observable.create(new Observable.OnSubscribe<T>() {
            @Override
            public void call(Subscriber<? super T> subscriber) {
                try {
                    subscriber.onNext(t);
                    subscriber.onCompleted();
                } catch (Exception e) {
                    subscriber.onError(e);
                }
            }
        });
    }
}
